package com.yjlove.myplay.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.yjlove.myplay.R;
import com.yjlove.myplay.base.BaseActivity;
import com.yjlove.myplay.common.PassAuthenticator;
import com.yjlove.myplay.common.StringPath;
import com.yjlove.myplay.utils.FileAndImageUtil;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


/**
 * 作者 YJlvoe
 * 创建时间 2017/3/29.
 */
public class EmailTestActivity extends BaseActivity implements View.OnClickListener {

    private Button mSend;
    private Button mError;
    private Button mShowBtn;
    private TextView mShowTxt;
    private String mPath = StringPath.LOG_PATH;

    @Override
    protected String getActivityTitle() {
        return getIntent().getStringExtra("title");
    }

    @Override
    protected int getActivityView() {
        return R.layout.activity_emailtest;
    }

    @Override
    protected void initView() {
        mSend = (Button) findViewById(R.id.email_send);
        mError = (Button) findViewById(R.id.email_error);
        mShowBtn = (Button) findViewById(R.id.email_show);
        mShowTxt = (TextView) findViewById(R.id.email_show_error);
    }

    @Override
    protected void initData() {
        mSend.setOnClickListener(this);
        mError.setOnClickListener(this);
        mShowBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.email_send:
                sendEmail();
                break;
            case R.id.email_error:
                int i = 0 / 0;
                break;
            case R.id.email_show:
                mShowTxt.setText(FileAndImageUtil.readFileContext(mPath + "crash.log"));
                break;
        }
    }

    /**
     * 发送邮件的方法
     * @return
     */
    private boolean sendEmail(){
        Properties props = new Properties();
        props.put("mail.smtp.protocol", "smtp");
        props.put("mail.smtp.auth", "true");//设置要验证
        props.put("mail.smtp.host", "smtp.163.com");//设置host
        props.put("mail.smtp.port", "25");  //设置端口
        PassAuthenticator pass = new PassAuthenticator();   //获取帐号密码
        Session session = Session.getInstance(props, pass); //获取验证会话
        try
        {
            //配置发送及接收邮箱
            InternetAddress fromAddress, toAddress;
            /**
             * 这个地方需要改成自己的邮箱
             */
            fromAddress = new InternetAddress("weichatappdemo@163.com", "weichatappdemo@163.com");
            toAddress   = new InternetAddress("weichatappdemo@163.com", "weichatappdemo@163.com");
            /**
             * 一下内容是：发送邮件时添加附件
             */
            MimeBodyPart attachPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(mPath + "crash.log"); //打开要发送的文件

            attachPart.setDataHandler(new DataHandler(fds));
            attachPart.setFileName(fds.getName());
            MimeMultipart allMultipart = new MimeMultipart("mixed"); //附件
            allMultipart.addBodyPart(attachPart);//添加
            //配置发送信息
            MimeMessage message = new MimeMessage(session);
//                message.setContent("test", "text/plain");
            message.setContent(allMultipart); //发邮件时添加附件
            message.setSubject("这次发送仅作测试");
            message.setFrom(fromAddress);
            message.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
            message.saveChanges();
            //连接邮箱并发送
            Transport transport = session.getTransport("smtp");
            /**
             * 这个地方需要改称自己的账号和密码
             */
            transport.connect("smtp.163.com", "weichatappdemo@163.com", "QQ114841207");
            transport.send(message);
            transport.close();
        } catch (Exception e) {
            //throw new RuntimeException();//将此异常向上抛出，此时CrashHandler就能够接收这里抛出的异常并最终将其存放到txt文件中
            //Log.e("sendmail", e.getMessage());
        }
        return false;
    }
}
